package model;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;

import net.ck.hzq.R;

import java.util.List;

import Bean.AllyBean;
import activity.glide.GlideApp;

public class Fragment1_allyAdapter extends BaseAdapter {

    private List<AllyBean.DataBean.ItemsBean> data;//数据

    private Context context;//上下文

    private String searchtype;

    private boolean mShowLevel;

    public Fragment1_allyAdapter(List<AllyBean.DataBean.ItemsBean> data, Context context, String searchtype, boolean showLevel) {
        this.data = data;
        this.context = context;
        this.searchtype = searchtype;
        mShowLevel = showLevel;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment1_allyadapter_item, null, false);
            viewHolder = new ViewHolder();
            viewHolder.relativeLayout = view.findViewById(R.id.f_a1_rela);
            viewHolder.imageView = view.findViewById(R.id.f_a1_img1);
            viewHolder.textView1 = view.findViewById(R.id.f_a1_txt1);
            viewHolder.textView2 = view.findViewById(R.id.f_a1_txt2);
            viewHolder.level = view.findViewById(R.id.level);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        GlideApp.with(context)
                .load(data.get(position).getBankBackImgPath())
                .error(R.drawable.photo)
                .placeholder(R.drawable.photo)
                .apply(RequestOptions.circleCropTransform())
                .into(viewHolder.imageView);
        viewHolder.textView1.setText(data.get(position).getRealName());
        if (searchtype.equals("0")) {
            viewHolder.textView2.setVisibility(View.GONE);
        } else if (searchtype.equals("1")) {
            viewHolder.textView2.setText(data.get(position).getAllyNumber() + "");
        } else if (searchtype.equals("2")) {
            viewHolder.textView2.setText(data.get(position).getMonTrad() + "");
        } else if (searchtype.equals("3")) {
            viewHolder.textView2.setText(data.get(position).getMonMerchant() + "");
        }

        if (data.get(position).currTrans != null) {
            switch (data.get(position).currTrans.levelAmount) {
                case 1:
                    viewHolder.level.setBackgroundResource(R.mipmap.r1);
                    break;
                case 2:
                    viewHolder.level.setBackgroundResource(R.mipmap.r2);
                    break;
                case 3:
                    viewHolder.level.setBackgroundResource(R.mipmap.r3);
                    break;
                case 4:
                    viewHolder.level.setBackgroundResource(R.mipmap.r4);
                    break;
                case 5:
                    viewHolder.level.setBackgroundResource(R.mipmap.r5);
                    break;
                case 6:
                    viewHolder.level.setBackgroundResource(R.mipmap.r6);
                    break;
                case 7:
                    viewHolder.level.setBackgroundResource(R.mipmap.r7);
                    break;
                case 8:
                    viewHolder.level.setBackgroundResource(R.mipmap.r8);
                    break;
                case 9:
                    viewHolder.level.setBackgroundResource(R.mipmap.r9);
                    break;
                case 10:
                    viewHolder.level.setBackgroundResource(R.mipmap.r10);
                    break;
                case 11:
                    viewHolder.level.setBackgroundResource(R.mipmap.r11);
                    break;
                case 12:
                    viewHolder.level.setBackgroundResource(R.mipmap.r12);
                    break;
                default:
                    break;

            }
        } else {
            viewHolder.level.setBackgroundResource(R.mipmap.r1);
        }
        viewHolder.level.setVisibility(mShowLevel ? View.VISIBLE : View.GONE);

        viewHolder.relativeLayout.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.get(position).getPhoneNumber()));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        });
        return view;
    }

    private class ViewHolder {
        RelativeLayout relativeLayout;
        ImageView imageView, level;
        TextView textView1, textView2;
    }

}
